package com.swpu.eduservice.service.impl;

import com.swpu.eduservice.Client.VodClient;
import com.swpu.eduservice.entity.EduVideo;
import com.swpu.eduservice.mapper.EduVideoMapper;
import com.swpu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author ygq
 * @since 2021-10-16
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Autowired
    private VodClient vodClient;

    @Override
    public void removeVedio(String courseId) {
        QueryWrapper<EduVideo> wa = new QueryWrapper<>();
        wa.eq("course_id",courseId);
        wa.select("video_source_id");
        List<EduVideo> eduVideos = baseMapper.selectList(wa);
        List<String> vedioIds = new ArrayList();
        for (EduVideo eduVideo : eduVideos) {
            if(!StringUtils.isEmpty(eduVideo.getVideoSourceId()))
            vedioIds.add(eduVideo.getVideoSourceId());
        }
        if(vedioIds.size()>0) vodClient.deletebatch(vedioIds);

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
